from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app

import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'

from google.appengine.dist import use_library
use_library('django', '1.2')

from google.appengine.ext.webapp import template

class MainHandler(webapp.RequestHandler):
  def get(self):
    template_values = {}
    path = os.path.join(os.path.dirname(__file__), 'hello.html')
    self.response.out.write(template.render(path, template_values))
   
class DiceHandler(webapp.RequestHandler):
  def get(self):
    times = self.request.get('times')
    import random
    times = self.request.get('times')
    if times.isdigit():   
       for roll_num in range(int(times)):
          dice_1 = random.randint(1, 6)
          dice_2 = random.randint(1, 6)
          rolls = str(dice_1) + ":" + str(dice_2)
          self.response.out.write(str(rolls) + "<br>")
          if dice_1 == 1 and dice_2 == 1:
             self.response.out.write("You've got snake eyes!" + "<br>")
          if dice_1 == 6 and dice_2 == 6:
             self.response.out.write("You got double sixes!" + "<br>")
    else:
       self.response.out.write("Choose a number.")
    #self.response.out.write('Hello ' + str(times))

class GoodbyeHandler(webapp.RequestHandler):
  def get(self):
    self.response.out.write('Goodbye app engine world!')

class QuizHandler(webapp.RequestHandler):
  def get(self):
    template_values = { 'name' : self.request.get('name') }
    path = os.path.join(os.path.dirname(__file__), 'quiz.html')
    self.response.out.write(template.render(path, template_values))

class WrongAnswer():
  def __init__(self, question, guess, correct):
    self.question = question
    self.guess = guess
    self.correct = correct

class RightAnswer():
  def __init__(self, question, guess, correct):
    self.question = question
    self.guess = guess
    self.correct = correct

class GradeQuizHandler(webapp.RequestHandler):
  def get(self):
    template_values = { 'wrong_answers' : [],
                        'right_answers' : [],
                        'name' : self.request.get('name') }
    addition_guess = self.request.get('addition').strip()
    capital_guess = self.request.get('capital').lower().strip()
    if addition_guess != '7':
      template_values['wrong_answers'].append(WrongAnswer("3 + 4", addition_guess, 7))
    else: 
      template_values['right_answers'].append(RightAnswer("3 + 4", addition_guess, 7))
    if capital_guess != 'sacramento':
      template_values['wrong_answers'].append(WrongAnswer("the capital of California", capital_guess, "Sacramento"))
    else: 
      template_values['right_answers'].append(RightAnswer("the capital of California", capital_guess, "Sacramento"))

    path = os.path.join(os.path.dirname(__file__), 'grade_quiz.html')
    self.response.out.write(template.render(path, template_values))

def main():
  application = webapp.WSGIApplication([('/hello.*', MainHandler),
                                        ('/goodbye.*', GoodbyeHandler),
                                        ('/dice.*', DiceHandler),
                                        ('/quiz', QuizHandler),
                                        ('/grade_quiz', GradeQuizHandler)],
                                       debug=True)
  run_wsgi_app(application)

if __name__ == '__main__':
  main()

